package com.itheima.controller;

import com.itheima.common.PaginationVO;
import com.itheima.common.Result;
import com.itheima.service.UserService;
import com.itheima.vo.UserInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/sys_user")
@Slf4j
public class UserController {
    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("/list")
    public Result<PaginationVO<UserInfoVO>> page(Integer limit, Integer page, String status, String login_name, String phone) {
        PaginationVO<UserInfoVO> pageBean = userService.page(limit, page, status, login_name, phone);
        return Result.success(pageBean);
    }

    /**
     * 添加用户
     *
     * @param user 使用者
     * @return {@link Result}<{@link ?}>
     */
    @PostMapping("/add")
    public Result<?> addUser(@RequestBody UserInfoVO user) {
        log.info("添加用户参数：{}", user);
        userService.addUser(user);
        return Result.success("");
    }

    /**
     * 更新用户
     *
     * @param user 使用者
     * @return {@link Result}<{@link ?}>
     */
    @PostMapping("/update")
    public Result<?> updateUser(@RequestBody UserInfoVO user) {
        log.info("修改用户参数：{}", user);
        userService.updateUser(user);
        return Result.success("");
    }

    /**
     * 行信息用户
     *
     * @param id id
     * @return {@link Result}<{@link UserInfoVO}>
     */
    @GetMapping("/rowInfo")
    public Result<UserInfoVO> rowInfoUser(String id) {
        log.info("用户详情参数：{}", id);
        UserInfoVO user = userService.rowInfoUser(id);
        return Result.success(user);
    }

    /**
     * del用户
     *
     * @param id id
     * @return {@link Result}<{@link ?}>
     */
    @GetMapping("/del")
    public Result<?> delUser(String id) {
        log.info("用户删除参数：{}", id);
        userService.delUser(id);
        return Result.success("");
    }
}
